package com.mmche.web.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.framework.base.util.JsoupUtil;
import com.framework.base.util.Tool;
import com.framework.support.domain.PageMan;
import com.framework.web.springmvc.BaseController;
import com.mmche.base.pojo.OnlineContent;
import com.mmche.base.pojo.ReadRecord;
import com.mmche.base.service.OnlineContentService;
import com.mmche.base.service.ReadRecordService;
import com.mmche.car.service.CarService;

@Controller
public class ArticleController extends BaseController{
	
	@Resource(name="webCarService")
	private CarService carService;
	
	@Resource(name="webOnlineContentService")
	private OnlineContentService onlineContentService;
	
	@Resource
	private ReadRecordService readRecordService;
	
	@RequestMapping("/gonglue/{id}.html")
	private String glInfo(@PathVariable String id , Map<String,Object> returnValue){
		return this.article(id, "gonglue", returnValue);
	}
	
	@RequestMapping("/zixun/{id}.html")
	private String zxInfo(@PathVariable String id , Map<String,Object> returnValue){
		return this.article(id, "zixun", returnValue);
	}
	
	private String article(String id,String type,Map<String,Object> returnValue){
		returnValue.put("hotCars",this.carService.getHotCars(5));
		OnlineContent content = this.onlineContentService.getContentById(id);
		if(content == null){
			id = id.toUpperCase();
			content = this.onlineContentService.getContentById(id);
		}
		returnValue.put("content",content);
		returnValue.put("prevContent",this.onlineContentService.getPrevGuide(content));
		returnValue.put("nextContent",this.onlineContentService.getNextGuide(content));
		this.onlineContentService.addContentCtr(content);
		//保存阅读记录
		ReadRecord readRecord = new ReadRecord();
		readRecord.setReadId(content.getId());
		readRecord.setType(ReadRecord.TYPE_CONTENT);
		readRecordService.saveReadRecord(readRecord);
		String title = content.getTitle()+"_麦麦车";
		String description  = content.getDescription();
		if(Tool.isEmpty(description))
			description = JsoupUtil.getHtmlFirstPart(content.getContent());//.this.getContentDescript(content.getContent());
		String keywords = content.getKeyword();
		if(type.equals("gonglue")){
			returnValue.put("hots", this.onlineContentService.getHotGuides());
		}
		if(type.equals("zixun")){
			//if(Tool.isEmpty(title))
			title = "二手车资讯_二手车新闻_麦麦车二手车";
			if(Tool.isEmpty(description))
				description="厦门麦麦车二手车网络超市资讯频道提供最专业及时的二手车新闻，麦麦车专家倾情奉献的二手车百事通，二手车评估案例，以及麦麦车独有的人物访谈和二手车专题。";
		    if(Tool.isEmpty(keywords))
		    		keywords="麦麦车,厦门二手车,二手车资讯,二手车热点,二手车政策,二手车动态,二手车新闻";
			returnValue.put("hots", this.onlineContentService.getHotNews());
		}
		returnValue.put("description",description);
		returnValue.put("title",title);
		returnValue.put("keywords",keywords);
		return "article/" + type + "_detail";
	}
	
	@RequestMapping("/gonglue/")
	private String gonglue(Map<String,Object> returnValue){
		return this.gonglue(returnValue, null);
	}

	@RequestMapping("/gonglue/all")
	private String gonglue(Map<String,Object> returnValue,String[] params){
		returnValue.put("hotGuides", this.onlineContentService.getHotGuides());
		returnValue.put("hotCars",this.carService.getHotCars(5));
		PageMan pageMan = new PageMan();
		pageMan.setPageSize(15);
		String pageNo = "";
		String type = "";
		if(!Tool.isEmpty(params)){
			for(String param : params){
				if(!Tool.isEmpty(param)){
					if(param.startsWith("P")){
						pageNo = param.substring(1);
						pageMan.setPageNo(Integer.parseInt(pageNo));
					}
					else if(!param.equals("all")){
						type = param;
					}
					else
						type = "all";
				}
			}
		}
		else
			type = "all";
		String type_url = "/TYPE" + (Tool.isEmpty(pageNo) ? "" : ("/P" + pageNo)) +"/";
		String page_url = (Tool.isEmpty(type) ? "" : ("/" + type)) + "/PPAGE" +"/";
		returnValue.put("type_url", type_url);
		returnValue.put("page_url", page_url);
		returnValue.put("type", type);
		returnValue.put("guides",this.onlineContentService.getGuides(pageMan));
		returnValue.put("pageMan",pageMan);
		return "article/gonglue";
	}
	
	@RequestMapping("/gonglue/{param1}")
	private String gonglue(@PathVariable String param1 , Map<String,Object> returnValue){
		return this.gonglue(returnValue, new String[]{param1});
	}
	
	@RequestMapping("/gonglue/{param1}/{param2}")
	private String gonglue(@PathVariable String param1 , @PathVariable String param2 , Map<String,Object> returnValue){
		return this.gonglue(returnValue, new String[]{param1,param2});
	}
	
	@RequestMapping("/zixun/")
	private String zixun(Map<String,Object> returnValue){
		return this.zixun(returnValue, null);
	}
	
	@RequestMapping("/zixun/all")
	private String zixun(Map<String,Object> returnValue,String[] params){
		returnValue.put("hotNews", this.onlineContentService.getHotNews());
		returnValue.put("hotCars",this.carService.getHotCars(5));
		PageMan pageMan = new PageMan();
		pageMan.setPageSize(15);
		String pageNo = "";
		String type = "";
		if(!Tool.isEmpty(params)){
			for(String param : params){
				if(!Tool.isEmpty(param)){
					if(param.startsWith("P")){
						pageNo = param.substring(1);
						pageMan.setPageNo(Integer.parseInt(pageNo));
					}
					else if(!param.equals("all")){
						type = param;
					}
					else
						type = "all";
				}
			}
		}
		else
			type = "all";
		returnValue.put("type", type);
		String type_url = "/TYPE" + (Tool.isEmpty(pageNo) ? "" : ("/P" + pageNo)) +"/";
		String page_url = (Tool.isEmpty(type) ? "" : ("/" + type)) + "/PPAGE" +"/";
		returnValue.put("type_url", type_url);
		returnValue.put("page_url", page_url);
		returnValue.put("news",this.onlineContentService.getNews(pageMan));
		returnValue.put("pageMan",pageMan);
		return "article/zixun";
	}
	
	@RequestMapping("/zixun/{param1}")
	private String zixun(@PathVariable String param1 , Map<String,Object> returnValue){
		return this.zixun(returnValue, new String[]{param1});
	}
	
	@RequestMapping("/zixun/{param1}/{param2}")
	private String zixun(@PathVariable String param1 , @PathVariable String param2 , Map<String,Object> returnValue){
		return this.zixun(returnValue, new String[]{param1,param2});
	}
}
